#include "JsonReader.hpp"


	JsonReader::JsonReader() {
            mRbcpConfigData = RbcpConfigData();
            readJSONFile();
	}


	JsonReader::~JsonReader() {

	}


void JsonReader::jsonPrettyPrint(Document & d) {
	StringBuffer buffer;
    PrettyWriter<StringBuffer> writer(buffer);
    d.Accept(writer);
    //console->warn("{:>30}", buffer.GetString());
    printf("%s\n", buffer.GetString());
}

void JsonReader::parseJSONFile(Document & d) {
	    int mode = 0;
    assert(d.HasMember("mode"));
    if (d["mode"].IsInt())
    {
    	mode = d["mode"].GetInt();
    }
    switch(mode) {
    	case 0:{
            assert(d.HasMember("asicTestMode"));
            Value & vMode = d["asicTestMode"];
            parseModeParg(vMode);
            break;
    	}
        case 1: {
            assert(d.HasMember("sampleTestMode"));
            Value & vMode = d["sampleTestMode"];
            parseModeParg(vMode);
            break;
        }
    	default: {
            assert(d.HasMember("realMode"));
            Value & vMode = d["realMode"];
            parseModeParg(vMode);
            break;
    	}

    }
    //printf("mode = %d\n", mode);

}

void JsonReader::parseModeParg(Value & vMode)
{
    assert(vMode.HasMember("Ipre"));
            if (vMode["Ipre"].IsInt())
            {
                //printf("Ipre %d\n", vMode["Ipre"].GetInt());
                mRbcpConfigData.mIpre = vMode["Ipre"].GetInt();
            }

            assert(vMode.HasMember("Isha"));
            if (vMode["Isha"].IsInt())
            {
                //printf("Isha %d\n", vMode["Isha"].GetInt());
                mRbcpConfigData.mIsha = vMode["Isha"].GetInt();
            }

            assert(vMode.HasMember("Ibuf"));
            if (vMode["Ibuf"].IsInt())
            {
                //printf("Ibuf %d\n", vMode["Ibuf"].GetInt());
                mRbcpConfigData.mIbuf = vMode["Ibuf"].GetInt();
            }

            assert(vMode.HasMember("Icomp"));
            if (vMode["Icomp"].IsInt())
            {
                //printf("Icomp %d\n", vMode["Icomp"].GetInt());
                mRbcpConfigData.mIcomp = vMode["Icomp"].GetInt();
            }

            assert(vMode.HasMember("Idriver"));
            if (vMode["Idriver"].IsInt())
            {
                //printf("Idriver %d\n", vMode["Idriver"].GetInt());
                mRbcpConfigData.mIdriver = vMode["Idriver"].GetInt();
            }

            assert(vMode.HasMember("Vfp"));
            if (vMode["Vfp"].IsInt())
            {
                //printf("Vfp %d\n", vMode["Vfp"].GetInt());
                mRbcpConfigData.mVfp = vMode["Vfp"].GetInt();
            }

            assert(vMode.HasMember("Vfs"));
            if (vMode["Vfs"].IsInt())
            {
                //printf("Vfs %d\n", vMode["Vfs"].GetInt());
                mRbcpConfigData.mVfs = vMode["Vfs"].GetInt();
            }

            assert(vMode.HasMember("Vref0"));
            if (vMode["Vref0"].IsInt())
            {
                //printf("Vref0 %d\n", vMode["Vref0"].GetInt());
                mRbcpConfigData.mVref0 = vMode["Vref0"].GetInt();
            }

            assert(vMode.HasMember("Vref1"));
            if (vMode["Vref1"].IsInt())
            {
                //printf("Vref1 %d\n", vMode["Vref1"].GetInt());
                mRbcpConfigData.mVref1 = vMode["Vref1"].GetInt();
            }
            assert(vMode.HasMember("Voffset"));
            if (vMode["Voffset"].IsInt())
            {
                //printf("Voffset %d\n", vMode["Voffset"].GetInt());
                mRbcpConfigData.mVoffset = vMode["Voffset"].GetInt();
            }

            assert(vMode.HasMember("Aout"));
            if (vMode["Aout"].IsInt())
            {
                //printf("Aout %d\n", vMode["Aout"].GetInt());
                mRbcpConfigData.mAout = vMode["Aout"].GetInt();
            }

            assert(vMode.HasMember("TpReg"));
            if (vMode["TpReg"].IsInt())
            {
                //printf("TpReg %d\n", vMode["TpReg"].GetInt());
                mRbcpConfigData.mTpReg = vMode["TpReg"].GetInt();
            }

            assert(vMode.HasMember("CReg"));
            if (vMode["CReg"].IsInt())
            {
                //printf("CReg %d\n", vMode["CReg"].GetInt());
                mRbcpConfigData.mCReg = vMode["CReg"].GetInt();
            }

            assert(vMode.HasMember("multiHitFilter"));
            if (vMode["multiHitFilter"].IsBool())
            {
                //printf("multiHitFilter %s\n", vMode["multiHitFilter"].GetBool()?"true":"false");
                mRbcpConfigData.multiHitFilter = vMode["multiHitFilter"].GetBool();
            }

            assert(vMode.HasMember("testMode"));
            if (vMode["testMode"].IsBool())
            {
                //printf("testMode %s\n", vMode["testMode"].GetBool()?"true":"false");
                mRbcpConfigData.testMode = vMode["testMode"].GetBool();
            }
}

void JsonReader::readJSONFile() {
	FILE* fp = fopen("../../src/config.json", "rb"); // 非 Windows 平台使用 "r"
	char readBuffer[65536];
	FileReadStream is(fp, readBuffer, sizeof(readBuffer));
	Document d;
	d.ParseStream(is);
	//jsonPrettyPrint(d);
	parseJSONFile(d);
	fclose(fp);
}

void JsonReader::show()
{
    printf("mIpre is %d\n", mRbcpConfigData.mIpre);
    printf("mIsha is %d\n", mRbcpConfigData.mIsha);
    printf("mIbuf is %d\n", mRbcpConfigData.mIbuf);
    printf("mIcomp is %d\n", mRbcpConfigData.mIcomp);
    printf("mIdriver is %d\n", mRbcpConfigData.mIdriver);
    printf("mVfp is %d\n", mRbcpConfigData.mVfp);
    printf("mVfs is %d\n", mRbcpConfigData.mVfs);
    printf("mVref0 is %d\n", mRbcpConfigData.mVref0);
    printf("mVref1 is %d\n", mRbcpConfigData.mVref1);
    printf("mVoffset is %d\n", mRbcpConfigData.mVoffset);
    printf("mAout is %d\n", mRbcpConfigData.mAout);
    printf("mTpReg is %d\n", mRbcpConfigData.mTpReg);
    printf("mCReg is %d\n", mRbcpConfigData.mCReg);
    printf("testMode is %d\n", mRbcpConfigData.testMode);
    printf("multiHitFilter is %d\n", mRbcpConfigData.multiHitFilter);
}
